import { _decorator, BoxCollider2D, CircleCollider2D, Color, Component, director, EventTouch, find, Input, instantiate, Node, PolygonCollider2D, Rect, RigidBody2D, Sprite, tween, UIOpacity, UITransform, Vec2, Vec3 } from 'cc';
import { ElementAction } from './ElementAction';
import { LayerRootAction } from './LayerRootAction';
import { Clips, events } from './Enums';
import { AudioManger } from './AudioManger';
import { LayerCatAction } from './LayerCatAction';
import { ColorDate } from './ColorDate';
import { Global } from './Global';
import { HoleAction } from './HoleAction';
const { ccclass, property } = _decorator;

@ccclass('PinAction')
export class PinAction extends Component {
    pin_color:ColorDate = null;
    @property({type:Sprite})
    pin_img:Sprite

    pos_hole:HoleAction = null;


    flying:boolean =false;

    start() {
        this.node.on(Input.EventType.TOUCH_START, this.touch_start, this);
    }

    update(deltaTime: number) {
        
    }

    public is_flying():boolean{
        return this.flying;
    }

    public set_flyinng(f:boolean){
        this.flying = f;
    }

    public init_date(group_id:number,pin_color:ColorDate,hole:HoleAction){
        this.pos_hole = hole;
        this.node.getComponent(RigidBody2D).group = group_id;
        this.node.getComponents(BoxCollider2D).forEach(element => {
            element.group = group_id;
        });
        this.node.getComponents(CircleCollider2D).forEach(element => {
            element.group = group_id;
        });
        this.node.getComponents(PolygonCollider2D).forEach(element => {
            element.group = group_id;
        });
        this.pin_color = pin_color;
        //set color
        this.reset_img();
    }

    touch_start(e: EventTouch) {
        if(Global.is_check_completed){
            console.log("---    is_check_completed  ---");
            return;
        }
        AudioManger.instance.playSound(Clips.pin_1);


        // ret?boolean ,return put result,true=success,false=failure
        if(Global.layer_cat_action.put_pin(this)){
            // 需要检查，关卡是否结束
            director.emit(events.check_guide, this);
            return;
        }
       Global.layer_empty_action.put_pin(this);

       if(Global.layer_empty_action.is_pin_full()){
            //TODO 游戏结束,这里有个bug,用延迟处理，后面处理
            Global.is_check_completed = true;
            tween(this.node).delay(1.4).call(()=>{
                if(Global.layer_empty_action.is_pin_full()){
                    console.log("---game over---");
                    Global.game_over_action.open();
                }else{
                    console.log("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
                }
                Global.is_check_completed = false;
            }).start();  
       }
       director.emit(events.check_guide, this);
        
    }


    public remove_collider(){
        this.node.getComponent(RigidBody2D).enabled =false;
        this.node.getComponent(CircleCollider2D).enabled = false;
        this.node.off(Input.EventType.TOUCH_START);
    }

    reset_img(){
        if(!this.pin_color){
            return;
        }
        // let img = this.node.getComponent(Sprite);
       // let img = this.node.getComponent(Sprite);
       this.pin_img.color = new Color(this.pin_color.r,this.pin_color.g,this.pin_color.b,255);

    //    let holering =  this.node.getChildByName("holering")
    //    if(holering){
    //     tween(holering)
    //     .to(0.35,{scale:new Vec3(1.2,1.2,1)},Global.our_easing)
    //     .to(0.35,{scale:new Vec3(0.8,0.8,1)},Global.our_easing)
    //     .to(0.25,{scale:new Vec3(1,1,1)},Global.our_easing)
    //     .call(()=>{
    //         holering.scale = new Vec3(1,1,1);
    //     })
    //     .start();
    //    }
       
    }

    // get_pin_color(arr:ColorDate[]){
    //     if(!this.pin_color){
    //         return;
    //     }
    //     arr.push(this.pin_color);
    // }

}

